package com.jsh.erp.controller;

import com.jsh.erp.datasource.entities.Transport;
import com.jsh.erp.datasource.vo.DepotHeadVo4List;
import com.jsh.erp.service.transport.TransportService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ji|sheng|hua 华夏erp
 */
@RestController
@RequestMapping(value = "/transport")
@Api(tags = {"运输公司管理"})
public class TransportController {
    private Logger logger = LoggerFactory.getLogger(TransportController.class);

    @Resource
    private TransportService transportService;
    @Resource
    private RedisService redisService;
    /**
     * 全部数据列表
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getAllList")
    @ApiOperation(value = "全部数据列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = Transport.class)
    })
    public BaseResponseInfo getAllList(HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Transport> transportList = transportService.getTransport();
            map.put("transportList", transportList);
            res.code = 200;
            res.data = transportList;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    /**
     * 根据Id获取地址信息
     * @param transportIds
     * @param request
     * @return
     */
    @GetMapping(value = "/getTransportByIds")
    @ApiOperation(value = "根据Id获取地址信息")
    public BaseResponseInfo getTransportByIds(@RequestParam("transportIds") String transportIds,
                                           HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Map<String,String> transportMap = transportService.getTransportMap();
            String names = transportService.getTransportByMapAndIds(transportMap, transportIds);
            map.put("names", names);
            res.code = 200;
            res.data = map;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

}
